<html><head><meta charset="utf-8"><title>20 SynchronousQueue 源码解析-慕课专栏</title>
			<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
			<meta name="renderer" content="webkit">
			<meta property="qc:admins" content="77103107776157736375">
			<meta property="wb:webmaster" content="c4f857219bfae3cb">
			<meta http-equiv="Access-Control-Allow-Origin" content="*">
			<meta http-equiv="Cache-Control" content="no-transform ">
			<meta http-equiv="Cache-Control" content="no-siteapp">
			<link rel="apple-touch-icon" sizes="76x76" href="https://www.imooc.com/static/img/common/touch-icon-ipad.png">
			<link rel="apple-touch-icon" sizes="120x120" href="https://www.imooc.com/static/img/common/touch-icon-iphone-retina.png">
			<link rel="apple-touch-icon" sizes="152x152" href="https://www.imooc.com/static/img/common/touch-icon-ipad-retina.png">
			<link href="https://moco.imooc.com/captcha/style/captcha.min.css" rel="stylesheet">
			<link rel="stylesheet" href="https://www.imooc.com/static/moco/v1.0/dist/css/moco.min.css?t=201907021539" type="text/css">
			<link rel="stylesheet" href="https://www.imooc.com/static/lib/swiper/swiper-3.4.2.min.css?t=201907021539">
			<link rel="stylesheet" href="https://static.mukewang.com/static/css/??base.css,common/common-less.css?t=2.5,column/zhuanlanChapter-less.css?t=2.5,course/inc/course_tipoff-less.css?t=2.5?v=201907051055" type="text/css">
			<link charset="utf-8" rel="stylesheet" href="https://www.imooc.com/static/lib/ueditor/themes/imooc/css/ueditor.css?v=201907021539"><link rel="stylesheet" href="https://www.imooc.com/static/lib/baiduShare/api/css/share_style0_16.css?v=6aba13f0.css"></head>
			<body><div id="main">


<div class="main-con hide-menu">
    <!-- 左侧菜单 & 索引 -->
    
    <div class="right-content" style="padding-left: 0px;">
        <div class="container clearfix" id="top" style="width: 1134px; display: block;">
            
            
            <div class="center_con js-center_con l" style="width: 1134px;">
                <div class="article-con">
                                            <!-- 买过的阅读 -->
                        

                    
                    <div class="art-title" style="margin-top: 0px;">
                        20 SynchronousQueue 源码解析
                    </div>
                    <div class="art-info clearfix">
                        
                        <span class="l">
                            更新时间：2019-10-15 11:19:50
                        </span>
                    </div>
                    <div class="art-top">
                                                <img src="https://img4.mukewang.com/5da483310001d01f06400359.jpg" alt="">
                                                                        <div class="famous-word-box">
                            <img src="https://www.imooc.com/static/img/column/bg-l.png" alt="" class="bg1 bg">
                            <img src="https://www.imooc.com/static/img/column/bg-r.png" alt="" class="bg2 bg">
                            <div class="famous-word">只有在那崎岖的小路上不畏艰险奋勇攀登的人,才有希望达到光辉的顶点。<p class="author">——马克思</p></div>
                        </div>
                                            </div>
                    <div class="art-content js-lookimg">
                        <div id="article_content">
                            <div class="cl-preview-section"><h2 id="引导语" style="font-size: 30px;">引导语</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">SynchronousQueue 是比较独特的队列，其本身是没有容量大小，比如我放一个数据到队列中，我是不能够立马返回的，我必须等待别人把我放进去的数据消费掉了，才能够返回。SynchronousQueue 在消息队列技术中间件中被大量使用，本文就来从底层实现来看下 SynchronousQueue 到底是如何做到的。</p>
</div><div class="cl-preview-section"><h2 id="整体架构" style="font-size: 30px;">1 整体架构</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">SynchronousQueue 的整体设计比较抽象，在内部抽象出了两种算法实现，一种是先入先出的队列，一种是后入先出的堆栈，两种算法被两个内部类实现，而直接对外的 put，take 方法的实现就非常简单，都是直接调用两个内部类的 transfer 方法进行实现，整体的调用关系如下图所示：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="https://img.mukewang.com/5da53a4a0001ce6924601406.png" data-original="//img.mukewang.com/5da53a4a0001ce6924601406.png" alt="图片描述"></p>
</div><div class="cl-preview-section"><h3 id="类注释">1.1 类注释</h3>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">源码的类注释往往能给我带来很多疑问和有用的信息，我们来看下类注释都说了什么：</p>
</div><div class="cl-preview-section"><ol>
<li style="font-size: 20px; line-height: 38px;">队列不存储数据，所以没有大小，也无法迭代；</li>
<li style="font-size: 20px; line-height: 38px;">插入操作的返回必须等待另一个线程完成对应数据的删除操作，反之亦然；</li>
<li style="font-size: 20px; line-height: 38px;">队列由两种数据结构组成，分别是后入先出的堆栈和先入先出的队列，堆栈是非公平的，队列是公平的。</li>
</ol>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">看到类注释，大家是不是有一些疑问，比如第二点是如何做到的？堆栈又是如何实现的呢？接下来我们一点一点揭晓。</p>
</div><div class="cl-preview-section"><h3 id="类图">1.2 类图</h3>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">SynchronousQueue 整体类图和 LinkedBlockingQueue 相似，都是实现了 BlockingQueue 接口，但因为其不储存数据结构，有一些方法是没有实现的，比如说 isEmpty、size、contains、remove 和迭代等方法，这些方法都是默认实现，如下截图：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="https://img.mukewang.com/5da53a3e0001e3c213841432.png" data-original="//img.mukewang.com/5da53a3e0001e3c213841432.png" alt="图片描述"></p>
</div><div class="cl-preview-section"><h3 id="结构细节">1.3 结构细节</h3>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">SynchronousQueue 底层结构和其它队列完全不同，有着独特的两种数据结构：队列和堆栈，我们一起来看下数据结构：</p>
</div><div class="cl-preview-section"><pre class=" language-java"><code class="prism  language-java">    <span class="token comment">// 堆栈和队列共同的接口</span>
    <span class="token comment">// 负责执行 put or take</span>
    <span class="token keyword">abstract</span> <span class="token keyword">static</span> <span class="token keyword">class</span> <span class="token class-name">Transferer</span><span class="token operator">&lt;</span>E<span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token comment">// e 为空的，会直接返回特殊值，不为空会传递给消费者</span>
        <span class="token comment">// timed 为 true，说明会有超时时间</span>
        <span class="token keyword">abstract</span> E <span class="token function">transfer</span><span class="token punctuation">(</span>E e<span class="token punctuation">,</span> <span class="token keyword">boolean</span> timed<span class="token punctuation">,</span> <span class="token keyword">long</span> nanos<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 堆栈 后入先出 非公平</span>
    <span class="token comment">// Scherer-Scott 算法</span>
    <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">TransferStack</span><span class="token operator">&lt;</span>E<span class="token operator">&gt;</span> <span class="token keyword">extends</span> <span class="token class-name">Transferer</span><span class="token operator">&lt;</span>E<span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 队列 先入先出 公平</span>
    <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">TransferQueue</span><span class="token operator">&lt;</span>E<span class="token operator">&gt;</span> <span class="token keyword">extends</span> <span class="token class-name">Transferer</span><span class="token operator">&lt;</span>E<span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">transient</span> <span class="token keyword">volatile</span> Transferer<span class="token operator">&lt;</span>E<span class="token operator">&gt;</span> transferer<span class="token punctuation">;</span>

    <span class="token comment">// 无参构造器默认为非公平的</span>
    <span class="token keyword">public</span> <span class="token function">SynchronousQueue</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> fair<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        transferer <span class="token operator">=</span> fair <span class="token operator">?</span> <span class="token keyword">new</span> <span class="token class-name">TransferQueue</span><span class="token operator">&lt;</span>E<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">TransferStack</span><span class="token operator">&lt;</span>E<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">从源码中我们可以得到几点：</p>
</div><div class="cl-preview-section"><ol>
<li style="font-size: 20px; line-height: 38px;">堆栈和队列都有一个共同的接口，叫做 Transferer，该接口有个方法：transfer，该方法很神奇，会承担 take 和 put 的双重功能；</li>
<li style="font-size: 20px; line-height: 38px;">在我们初始化的时候，是可以选择是使用堆栈还是队列的，如果你不选择，默认的就是堆栈，类注释中也说明了这一点，堆栈的效率比队列更高。</li>
</ol>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">接下来我们来看下堆栈和队列的具体实现。</p>
</div><div class="cl-preview-section"><h2 id="非公平的堆栈" style="font-size: 30px;">2 非公平的堆栈</h2>
</div><div class="cl-preview-section"><h3 id="堆栈的结构">2.1 堆栈的结构</h3>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">首先我们来介绍下堆栈的整体结构，如下：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="https://img.mukewang.com/5da53a31000147a007280674.png" data-original="//img.mukewang.com/5da53a31000147a007280674.png" alt="图片描述"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">从上图中我们可以看到，我们有一个大的堆栈池，池的开口叫做堆栈头，put 的时候，就往堆栈池中放数据。take 的时候，就从堆栈池中拿数据，两者操作都是在堆栈头上操作数据，从图中可以看到，越靠近堆栈头，数据越新，所以每次 take 的时候，都会拿到堆栈头的最新数据，这就是我们说的后入先出，也就是非公平的。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">图中 SNode 就是源码中栈元素的表示，我们看下源码：</p>
</div><div class="cl-preview-section"><pre class=" language-java"><code class="prism  language-java"><span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">SNode</span> <span class="token punctuation">{</span>
    <span class="token comment">// 栈的下一个，就是被当前栈压在下面的栈元素</span>
    <span class="token keyword">volatile</span> SNode next<span class="token punctuation">;</span>
    <span class="token comment">// 节点匹配，用来判断阻塞栈元素能被唤醒的时机</span>
    <span class="token comment">// 比如我们先执行 take，此时队列中没有数据，take 被阻塞了，栈元素为 SNode1</span>
    <span class="token comment">// 当有 put 操作时，会把当前 put 的栈元素赋值给 SNode1 的 match 属性，并唤醒 take 操作</span>
    <span class="token comment">// 当 take 被唤醒，发现 SNode1 的 match 属性有值时，就能拿到 put 进来的数据，从而返回</span>
    <span class="token keyword">volatile</span> SNode match<span class="token punctuation">;</span>
    <span class="token comment">// 栈元素的阻塞是通过线程阻塞来实现的，waiter 为阻塞的线程</span>
    <span class="token keyword">volatile</span> Thread waiter<span class="token punctuation">;</span>
    <span class="token comment">// 未投递的消息，或者未消费的消息</span>
    Object item<span class="token punctuation">;</span>             
<span class="token punctuation">}</span> 
</code></pre>
</div><div class="cl-preview-section"><h3 id="入栈和出栈">2.2 入栈和出栈</h3>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">入栈指的是使用 put 等方法，把数据放到堆栈池中，出栈指的使用 take 等方法，把数据从堆栈池中拿出来，操作的对象都是堆栈头，虽然两者的一个是从堆栈头拿数据，一个是放数据，但底层实现的方法却是同一个，源码如下：</p>
</div><div class="cl-preview-section"><pre class=" language-java"><code class="prism  language-java"><span class="token comment">// transfer 方法思路比较复杂，因为 take 和 put 两个方法都揉在了一起</span>
<span class="token annotation punctuation">@SuppressWarnings</span><span class="token punctuation">(</span><span class="token string">"unchecked"</span><span class="token punctuation">)</span>
E <span class="token function">transfer</span><span class="token punctuation">(</span>E e<span class="token punctuation">,</span> <span class="token keyword">boolean</span> timed<span class="token punctuation">,</span> <span class="token keyword">long</span> nanos<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    SNode s <span class="token operator">=</span> null<span class="token punctuation">;</span> <span class="token comment">// constructed/reused as needed</span>
    <span class="token comment">// e 为空，说明是 take 方法，不为空是 put 方法</span>
    <span class="token keyword">int</span> mode <span class="token operator">=</span> <span class="token punctuation">(</span>e <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token operator">?</span> REQUEST <span class="token operator">:</span> DATA<span class="token punctuation">;</span>
    <span class="token comment">// 自旋</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span><span class="token punctuation">;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 拿出头节点，有几种情况</span>
        <span class="token comment">// 1：头节点为空，说明队列中还没有数据</span>
        <span class="token comment">// 2：头节点不为空，并且是 take 类型的，说明头节点线程正等着拿数据。</span>
        <span class="token comment">// 3：头节点不为空，并且是 put 类型的，说明头节点线程正等着放数据。</span>
        SNode h <span class="token operator">=</span> head<span class="token punctuation">;</span>
        <span class="token comment">// 栈头为空，说明队列中还没有数据。</span>
        <span class="token comment">// 栈头不为空，并且栈头的类型和本次操作一致，比如都是 put，那么就把</span>
        <span class="token comment">// 本次 put 操作放到该栈头的前面即可，让本次 put 能够先执行</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>h <span class="token operator">==</span> null <span class="token operator">||</span> h<span class="token punctuation">.</span>mode <span class="token operator">==</span> mode<span class="token punctuation">)</span> <span class="token punctuation">{</span>  <span class="token comment">// empty or same-mode</span>
            <span class="token comment">// 设置了超时时间，并且 e 进栈或者出栈要超时了，</span>
            <span class="token comment">// 就会丢弃本次操作，返回 null 值。</span>
            <span class="token comment">// 如果栈头此时被取消了，丢弃栈头，取下一个节点继续消费</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>timed <span class="token operator">&amp;&amp;</span> nanos <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>      <span class="token comment">// can't wait</span>
                <span class="token comment">// 栈头操作被取消</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>h <span class="token operator">!=</span> null <span class="token operator">&amp;&amp;</span> h<span class="token punctuation">.</span><span class="token function">isCancelled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                    <span class="token comment">// 丢弃栈头，把栈头后一个元素作为栈头</span>
                    <span class="token function">casHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> h<span class="token punctuation">.</span>next<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// pop cancelled node</span>
                <span class="token comment">//栈头是空的，直接返回 null</span>
                <span class="token keyword">else</span>
                    <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token comment">// 没有超时，直接把 e 作为新的栈头</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">casHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> s <span class="token operator">=</span> <span class="token function">snode</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> e<span class="token punctuation">,</span> h<span class="token punctuation">,</span> mode<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// e 等待出栈，一种是空队列 take，一种是 put</span>
                SNode m <span class="token operator">=</span> <span class="token function">awaitFulfill</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> timed<span class="token punctuation">,</span> nanos<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>m <span class="token operator">==</span> s<span class="token punctuation">)</span> <span class="token punctuation">{</span>               <span class="token comment">// wait was cancelled</span>
                    <span class="token function">clean</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">return</span> null<span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token comment">// 本来 s 是栈头的，现在 s 不是栈头了，s 后面又来了一个数，把新的数据作为栈头</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>h <span class="token operator">=</span> head<span class="token punctuation">)</span> <span class="token operator">!=</span> null <span class="token operator">&amp;&amp;</span> h<span class="token punctuation">.</span>next <span class="token operator">==</span> s<span class="token punctuation">)</span>
                    <span class="token function">casHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> s<span class="token punctuation">.</span>next<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// help s's fulfiller</span>
                <span class="token keyword">return</span> <span class="token punctuation">(</span>E<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>mode <span class="token operator">==</span> REQUEST<span class="token punctuation">)</span> <span class="token operator">?</span> m<span class="token punctuation">.</span>item <span class="token operator">:</span> s<span class="token punctuation">.</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token comment">// 栈头正在等待其他线程 put 或 take</span>
        <span class="token comment">// 比如栈头正在阻塞，并且是 put 类型，而此次操作正好是 take 类型，走此处</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">isFulfilling</span><span class="token punctuation">(</span>h<span class="token punctuation">.</span>mode<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// try to fulfill</span>
            <span class="token comment">// 栈头已经被取消，把下一个元素作为栈头</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>h<span class="token punctuation">.</span><span class="token function">isCancelled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>            <span class="token comment">// already cancelled</span>
                <span class="token function">casHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> h<span class="token punctuation">.</span>next<span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">// pop and retry</span>
            <span class="token comment">// snode 方法第三个参数 h 代表栈头，赋值给 s 的 next 属性</span>
            <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">casHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> s<span class="token operator">=</span><span class="token function">snode</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> e<span class="token punctuation">,</span> h<span class="token punctuation">,</span> FULFILLING<span class="token operator">|</span>mode<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span><span class="token punctuation">;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// loop until matched or waiters disappear</span>
                    <span class="token comment">// m 就是栈头，通过上面 snode 方法刚刚赋值</span>
                    SNode m <span class="token operator">=</span> s<span class="token punctuation">.</span>next<span class="token punctuation">;</span>       <span class="token comment">// m is s's match</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>m <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span>        <span class="token comment">// all waiters are gone</span>
                        <span class="token function">casHead</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> null<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// pop fulfill node</span>
                        s <span class="token operator">=</span> null<span class="token punctuation">;</span>           <span class="token comment">// use new node next time</span>
                        <span class="token keyword">break</span><span class="token punctuation">;</span>              <span class="token comment">// restart main loop</span>
                    <span class="token punctuation">}</span>
                    SNode mn <span class="token operator">=</span> m<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
                     <span class="token comment">// tryMatch 非常重要的方法，两个作用：</span>
                     <span class="token comment">// 1 唤醒被阻塞的栈头 m，2 把当前节点 s 赋值给 m 的 match 属性</span>
                     <span class="token comment">// 这样栈头 m 被唤醒时，就能从 m.match 中得到本次操作 s</span>
                     <span class="token comment">// 其中 s.item 记录着本次的操作节点，也就是记录本次操作的数据</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">tryMatch</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token function">casHead</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> mn<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// pop both s and m</span>
                        <span class="token keyword">return</span> <span class="token punctuation">(</span>E<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>mode <span class="token operator">==</span> REQUEST<span class="token punctuation">)</span> <span class="token operator">?</span> m<span class="token punctuation">.</span>item <span class="token operator">:</span> s<span class="token punctuation">.</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">else</span>                  <span class="token comment">// lost match</span>
                        s<span class="token punctuation">.</span><span class="token function">casNext</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> mn<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// help unlink</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>                            <span class="token comment">// help a fulfiller</span>
            SNode m <span class="token operator">=</span> h<span class="token punctuation">.</span>next<span class="token punctuation">;</span>               <span class="token comment">// m is h's match</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>m <span class="token operator">==</span> null<span class="token punctuation">)</span>                  <span class="token comment">// waiter is gone</span>
                <span class="token function">casHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> null<span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">// pop fulfilling node</span>
            <span class="token keyword">else</span> <span class="token punctuation">{</span>
                SNode mn <span class="token operator">=</span> m<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">tryMatch</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span><span class="token punctuation">)</span>          <span class="token comment">// help match</span>
                    <span class="token function">casHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> mn<span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">// pop both h and m</span>
                <span class="token keyword">else</span>                        <span class="token comment">// lost match</span>
                    h<span class="token punctuation">.</span><span class="token function">casNext</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> mn<span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// help unlink</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">从源码中密密麻麻的注释，我们就可以看出来此方法比较复杂，我们总结一下大概的操作思路：</p>
</div><div class="cl-preview-section"><ol>
<li style="font-size: 20px; line-height: 38px;">判断是 put 方法还是 take 方法；</li>
<li style="font-size: 20px; line-height: 38px;">判断栈头数据是否为空，如果为空或者栈头的操作和本次操作一致，是的话走 3，否则走 5；</li>
<li style="font-size: 20px; line-height: 38px;">判断操作有无设置超时时间，如果设置了超时时间并且已经超时，返回 null，否则走 4；</li>
<li style="font-size: 20px; line-height: 38px;">如果栈头为空，把当前操作设置成栈头，或者栈头不为空，但栈头的操作和本次操作相同，也把当前操作设置成栈头，并看看其它线程能否满足自己，不能满足则阻塞自己。比如当前操作是 take，但队列中没有数据，则阻塞自己；</li>
<li style="font-size: 20px; line-height: 38px;">如果栈头已经是阻塞住的，需要别人唤醒的，判断当前操作能否唤醒栈头，可以唤醒走 6，否则走 4；</li>
<li style="font-size: 20px; line-height: 38px;">把自己当作一个节点，赋值到栈头的 match 属性上，并唤醒栈头节点；</li>
<li style="font-size: 20px; line-height: 38px;">栈头被唤醒后，拿到 match 属性，就是把自己唤醒的节点的信息，返回。</li>
</ol>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">在整个过程中，有一个节点阻塞的方法，实现原理如下：</p>
</div><div class="cl-preview-section"><pre class=" language-java"><code class="prism  language-java">SNode <span class="token function">awaitFulfill</span><span class="token punctuation">(</span>SNode s<span class="token punctuation">,</span> <span class="token keyword">boolean</span> timed<span class="token punctuation">,</span> <span class="token keyword">long</span> nanos<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// deadline 死亡时间，如果设置了超时时间的话，死亡时间等于当前时间 + 超时时间，否则就是 0</span>
    <span class="token keyword">final</span> <span class="token keyword">long</span> deadline <span class="token operator">=</span> timed <span class="token operator">?</span> System<span class="token punctuation">.</span><span class="token function">nanoTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> nanos <span class="token operator">:</span> 0L<span class="token punctuation">;</span>
    Thread w <span class="token operator">=</span> Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 自旋的次数，如果设置了超时时间，会自旋 32 次，否则自旋 512 次。</span>
    <span class="token comment">// 比如本次操作是 take 操作，自选次数后，仍没有其他线程 put 数据进来</span>
    <span class="token comment">// 就会阻塞，有超时时间的，会阻塞固定的时间，否则一致阻塞下去</span>
    <span class="token keyword">int</span> spins <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token function">shouldSpin</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token operator">?</span>
                 <span class="token punctuation">(</span>timed <span class="token operator">?</span> maxTimedSpins <span class="token operator">:</span> maxUntimedSpins<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span><span class="token punctuation">;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 当前线程有无被打断，如果过了超时时间，当前线程就会被打断</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>w<span class="token punctuation">.</span><span class="token function">isInterrupted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            s<span class="token punctuation">.</span><span class="token function">tryCancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        SNode m <span class="token operator">=</span> s<span class="token punctuation">.</span>match<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>m <span class="token operator">!=</span> null<span class="token punctuation">)</span>
            <span class="token keyword">return</span> m<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>timed<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            nanos <span class="token operator">=</span> deadline <span class="token operator">-</span> System<span class="token punctuation">.</span><span class="token function">nanoTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 超时了，取消当前线程的等待操作</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>nanos <span class="token operator">&lt;=</span> 0L<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                s<span class="token punctuation">.</span><span class="token function">tryCancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">continue</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 自选次数减少 1</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>spins <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
            spins <span class="token operator">=</span> <span class="token function">shouldSpin</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token punctuation">(</span>spins<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token comment">// 把当前线程设置成 waiter，主要是通过线程来完成阻塞和唤醒</span>
        <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>s<span class="token punctuation">.</span>waiter <span class="token operator">==</span> null<span class="token punctuation">)</span>
            s<span class="token punctuation">.</span>waiter <span class="token operator">=</span> w<span class="token punctuation">;</span> <span class="token comment">// establish waiter so can park next iter</span>
        <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>timed<span class="token punctuation">)</span>
            <span class="token comment">// 通过 park 进行阻塞，这个我们在锁章节中会说明</span>
            LockSupport<span class="token punctuation">.</span><span class="token function">park</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nanos <span class="token operator">&gt;</span> spinForTimeoutThreshold<span class="token punctuation">)</span>
            LockSupport<span class="token punctuation">.</span><span class="token function">parkNanos</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> nanos<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">从节点阻塞代码中，我们可以发现，其阻塞的策略，并不是一上来就阻塞住，而是在自旋一定次数后，仍然没有其它线程来满足自己的要求时，才会真正的阻塞住。</p>
</div><div class="cl-preview-section"><h2 id="公平的队列" style="font-size: 30px;">3 公平的队列</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">首先我们来看一下队列中的每个元素的组成：</p>
</div><div class="cl-preview-section"><pre class=" language-java"><code class="prism  language-java"><span class="token comment">/** 队列头 */</span>
<span class="token keyword">transient</span> <span class="token keyword">volatile</span> QNode head<span class="token punctuation">;</span>
<span class="token comment">/** 队列尾 */</span>
<span class="token keyword">transient</span> <span class="token keyword">volatile</span> QNode tail<span class="token punctuation">;</span>

<span class="token comment">// 队列的元素</span>
<span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">QNode</span> <span class="token punctuation">{</span>
    <span class="token comment">// 当前元素的下一个元素</span>
    <span class="token keyword">volatile</span> QNode next<span class="token punctuation">;</span>         
    <span class="token comment">// 当前元素的值，如果当前元素被阻塞住了，等其他线程来唤醒自己时，其他线程</span>
    <span class="token comment">// 会把自己 set 到 item 里面</span>
    <span class="token keyword">volatile</span> Object item<span class="token punctuation">;</span>         <span class="token comment">// CAS'ed to or from null</span>
    <span class="token comment">// 可以阻塞住的当前线程</span>
    <span class="token keyword">volatile</span> Thread waiter<span class="token punctuation">;</span>       <span class="token comment">// to control park/unpark</span>
    <span class="token comment">// true 是 put，false 是 take</span>
    <span class="token keyword">final</span> <span class="token keyword">boolean</span> isData<span class="token punctuation">;</span>
<span class="token punctuation">}</span>  
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">公平的队列主要使用的是 TransferQueue 内部类的 transfer 方法，我们一起来看下源码：</p>
</div><div class="cl-preview-section"><pre class=" language-java"><code class="prism  language-java">E <span class="token function">transfer</span><span class="token punctuation">(</span>E e<span class="token punctuation">,</span> <span class="token keyword">boolean</span> timed<span class="token punctuation">,</span> <span class="token keyword">long</span> nanos<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    QNode s <span class="token operator">=</span> null<span class="token punctuation">;</span> <span class="token comment">// constructed/reused as needed</span>
    <span class="token comment">// true 是 put，false 是 get</span>
    <span class="token keyword">boolean</span> isData <span class="token operator">=</span> <span class="token punctuation">(</span>e <span class="token operator">!=</span> null<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span><span class="token punctuation">;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 队列头和尾的临时变量,队列是空的时候，t=h</span>
        QNode t <span class="token operator">=</span> tail<span class="token punctuation">;</span>
        QNode h <span class="token operator">=</span> head<span class="token punctuation">;</span>
        <span class="token comment">// tail 和 head 没有初始化时，无限循环</span>
        <span class="token comment">// 虽然这种 continue 非常耗cpu，但感觉不会碰到这种情况</span>
        <span class="token comment">// 因为 tail 和 head 在 TransferQueue 初始化的时候，就已经被赋值空节点了</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator">==</span> null <span class="token operator">||</span> h <span class="token operator">==</span> null<span class="token punctuation">)</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token comment">// 首尾节点相同，说明是空队列</span>
        <span class="token comment">// 或者尾节点的操作和当前节点操作一致</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>h <span class="token operator">==</span> t <span class="token operator">||</span> t<span class="token punctuation">.</span>isData <span class="token operator">==</span> isData<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            QNode tn <span class="token operator">=</span> t<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
            <span class="token comment">// 当 t 不是 tail 时，说明 tail 已经被修改过了</span>
            <span class="token comment">// 因为 tail 没有被修改的情况下，t 和 tail 必然相等</span>
            <span class="token comment">// 因为前面刚刚执行赋值操作： t = tail</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator">!=</span> tail<span class="token punctuation">)</span>
                <span class="token keyword">continue</span><span class="token punctuation">;</span>
            <span class="token comment">// 队尾后面的值还不为空，t 还不是队尾，直接把 tn 赋值给 t，这是一步加强校验。</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>tn <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">advanceTail</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> tn<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">continue</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment">//超时直接返回 null</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>timed <span class="token operator">&amp;&amp;</span> nanos <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span>        <span class="token comment">// can't wait</span>
                <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token comment">//构造node节点</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>s <span class="token operator">==</span> null<span class="token punctuation">)</span>
                s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QNode</span><span class="token punctuation">(</span>e<span class="token punctuation">,</span> isData<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//如果把 e 放到队尾失败，继续递归放进去</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>t<span class="token punctuation">.</span><span class="token function">casNext</span><span class="token punctuation">(</span>null<span class="token punctuation">,</span> s<span class="token punctuation">)</span><span class="token punctuation">)</span>        <span class="token comment">// failed to link in</span>
                <span class="token keyword">continue</span><span class="token punctuation">;</span>

            <span class="token function">advanceTail</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> s<span class="token punctuation">)</span><span class="token punctuation">;</span>              <span class="token comment">// swing tail and wait</span>
            <span class="token comment">// 阻塞住自己</span>
            Object x <span class="token operator">=</span> <span class="token function">awaitFulfill</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> e<span class="token punctuation">,</span> timed<span class="token punctuation">,</span> nanos<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">==</span> s<span class="token punctuation">)</span> <span class="token punctuation">{</span>                   <span class="token comment">// wait was cancelled</span>
                <span class="token function">clean</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> s<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> null<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>s<span class="token punctuation">.</span><span class="token function">isOffList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>           <span class="token comment">// not already unlinked</span>
                <span class="token function">advanceHead</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> s<span class="token punctuation">)</span><span class="token punctuation">;</span>          <span class="token comment">// unlink if head</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">!=</span> null<span class="token punctuation">)</span>              <span class="token comment">// and forget fields</span>
                    s<span class="token punctuation">.</span>item <span class="token operator">=</span> s<span class="token punctuation">;</span>
                s<span class="token punctuation">.</span>waiter <span class="token operator">=</span> null<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> <span class="token punctuation">(</span>x <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token punctuation">(</span>E<span class="token punctuation">)</span>x <span class="token operator">:</span> e<span class="token punctuation">;</span>
        <span class="token comment">// 队列不为空，并且当前操作和队尾不一致</span>
        <span class="token comment">// 也就是说当前操作是队尾是对应的操作</span>
        <span class="token comment">// 比如说队尾是因为 take 被阻塞的，那么当前操作必然是 put</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>                            <span class="token comment">// complementary-mode</span>
            <span class="token comment">// 如果是第一次执行，此处的 m 代表就是 tail</span>
            <span class="token comment">// 也就是这行代码体现出队列的公平，每次操作时，从头开始按照顺序进行操作</span>
            QNode m <span class="token operator">=</span> h<span class="token punctuation">.</span>next<span class="token punctuation">;</span>               <span class="token comment">// node to fulfill</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator">!=</span> tail <span class="token operator">||</span> m <span class="token operator">==</span> null <span class="token operator">||</span> h <span class="token operator">!=</span> head<span class="token punctuation">)</span>
                <span class="token keyword">continue</span><span class="token punctuation">;</span>                   <span class="token comment">// inconsistent read</span>

            Object x <span class="token operator">=</span> m<span class="token punctuation">.</span>item<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>isData <span class="token operator">==</span> <span class="token punctuation">(</span>x <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token operator">||</span>    <span class="token comment">// m already fulfilled</span>
                x <span class="token operator">==</span> m <span class="token operator">||</span>                   <span class="token comment">// m cancelled</span>
                <span class="token comment">// m 代表栈头</span>
                <span class="token comment">// 这里把当前的操作值赋值给阻塞住的 m 的 item 属性</span>
                <span class="token comment">// 这样 m 被释放时，就可得到此次操作的值</span>
                <span class="token operator">!</span>m<span class="token punctuation">.</span><span class="token function">casItem</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>         <span class="token comment">// lost CAS</span>
                <span class="token function">advanceHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">;</span>          <span class="token comment">// dequeue and retry</span>
                <span class="token keyword">continue</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment">// 当前操作放到队头</span>
            <span class="token function">advanceHead</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">;</span>              <span class="token comment">// successfully fulfilled</span>
            <span class="token comment">// 释放队头阻塞节点</span>
            LockSupport<span class="token punctuation">.</span><span class="token function">unpark</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span>waiter<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token punctuation">(</span>x <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token punctuation">(</span>E<span class="token punctuation">)</span>x <span class="token operator">:</span> e<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">源码比较复杂，我们需要搞清楚的是，线程被阻塞住后，当前线程是如何把自己的数据传给阻塞线程的。为了方便说明，我们假设线程 1 往队列中 take 数据 ，被阻塞住了，变成阻塞线程 A ，然后线程 2 开始往队列中 put 数据 B，大致的流程是这样的：</p>
</div><div class="cl-preview-section"><ol>
<li style="font-size: 20px; line-height: 38px;">线程 1 从队列中拿数据，发现队列中没有数据，于是被阻塞，成为 A ；</li>
<li style="font-size: 20px; line-height: 38px;">线程 2 往队尾 put 数据，会从队尾往前找到第一个被阻塞的节点，假设此时能找到的就是节点 A，然后线程 B 把将 put 的数据放到节点 A 的 item 属性里面，并唤醒线程 1；</li>
<li style="font-size: 20px; line-height: 38px;">线程 1 被唤醒后，就能从 A.item 里面拿到线程 2 put 的数据了，线程 1 成功返回。</li>
</ol>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">从这个过程中，我们能看出公平主要体现在，每次 put 数据的时候，都 put 到队尾上，而每次拿数据时，并不是直接从堆头拿数据，而是从队尾往前寻找第一个被阻塞的线程，这样就会按照顺序释放被阻塞的线程。</p>
</div><div class="cl-preview-section"><h2 id="总结" style="font-size: 30px;">4 总结</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">SynchronousQueue 源码比较复杂，建议大家进行源码的 debug 来学习源码，为大家准备了调试类：SynchronousQueueDemo，大家可以下载源码自己调试一下，这样学起来应该会更加轻松一点。</p>
</div>}
                        </div>
                    </div>
                                            <!-- 买过的阅读 -->
                        <div class="art-next-prev clearfix">
                                                                                                <!-- 已买且开放 或者可以试读 -->
                                    <a href="/read/47/article/861">
                                                                    <div class="prev l clearfix">
                                        <div class="icon l">
                                            <i class="imv2-arrow3_l"></i>
                                        </div>
                                        <p>
                                            19 LinkedBlockingQueue 源码解析
                                        </p>
                                    </div>
                                </a>
                                                                                                                            <!-- 已买且开放 或者可以试读 -->
                                    <a href="/read/47/article/863">
                                                                    <div class="next r clearfix">
                                        <p>
                                            21 DelayQueue 源码解析  
                                        </p>
                                        <div class="icon r">
                                            <i class="imv2-arrow3_r"></i>
                                        </div>

                                    </div>
                                </a>
                                                    </div>
                                    </div>
                <div class="comments-con js-comments-con" id="coments_con">
                </div>

                
            </div>
            
            
            

        </div>
    </div>
</div>

<div class="modal modal-jiaQun-new hide" id="modal-jiaQun">
    <div class="inner" style="">
        <div class="modal-close js-close-jiaQun">
            <i class="imv2-close"></i>
        </div>
        <div class="content">
            <img src="https://img2.mukewang.com/5d634d40000119e505400602.jpg">
            <div class="right-info">
                <div class="title">
                    扫码加入慕课Java核心用户群
                </div>
                <div class="desc">
                                            <p class="mb6">验证信息：<span id="joincode">1909271435058473</span><span class="copy js-copy-joincode">复制</span></p>
                                        <p class="mb6">QQ讨论群号：906691736</p>
                                            <p>QQ群URL：<a href="https://jq.qq.com/?_wv=1027&amp;k=55RtSbJ" target="_blank">点击访问</a></p>
                                    </div>
            </div>
            <p class="tip">若遇到搜索不到QQ群或加群失败，请联系客服邮箱:kf@imooc.com</p>
        </div>
    </div>
</div>
 
<!-- 专栏介绍页专栏评价 -->

<!-- 专栏介绍页底部三条评价 -->

<!-- 专栏阅读页弹层目录和介绍页页面目录 -->

<!-- 专栏阅读页发布回复 -->

<!-- 专栏阅读页发布评论 -->

<!-- 专栏阅读页底部评论 -->

<!-- 专栏阅读 单个 评论 -->

<!-- 新增回复和展开三条以外回复 -->

<!-- 立即订阅的弹窗 -->












</div></body></html>